﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace GetAveragePopulation
{
    class Program
    {
        static void Main(string[] args)
        {

            string destFileEN = @".\stats_en.txt";
            string destFileFR = @".\stats_fr.txt";
            string destFileDE = @".\stats_de.txt";
            string originFileEN = @".\..\..\..\Texts\EN\en_";
            string originFileFR = @".\..\..\..\Texts\FR\fr_";
            string originFileDE = @".\..\..\..\Texts\DE\de_";
            string[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "w", "z", "y", "ä", "ü", "ö", "ß" };
            StringBuilder occurrences = new StringBuilder();

            GetStats(destFileEN, originFileEN, letters, occurrences);
            Console.WriteLine("Analise de textos em ingles concluida.");
            GetStats(destFileFR, originFileFR, letters, occurrences);
            Console.WriteLine("Analise de textos em frances concluida.");
            GetStats(destFileDE, originFileDE, letters, occurrences);
            Console.WriteLine("Analise de textos em alemao concluida.");

            //string[] characteristics = { "t", "the", "to", "and", "of", "s", "la", "et", "le", "r", "n", "ö", "ü", "die", "und" };
            //GetStats(destFileEN, originFileEN, characteristics, occurrences);
            //Console.WriteLine("Analise de caracteristicas em ingles concluida.");
            //GetStats(destFileFR, originFileFR, characteristics, occurrences);
            //Console.WriteLine("Analise de caracteristicas em frances concluida.");
            //GetStats(destFileDE, originFileDE, characteristics, occurrences);
            //Console.WriteLine("Analise de caracteristicas em alemao concluida.");

            Console.ReadLine();

        }

        private static void GetStats(string destFileEN, string originFileEN, string[] letters, StringBuilder occurrences)
        {

            occurrences.Clear();

            for (int i = 1; i < 11; i++)
            {
                string text;
                using (System.IO.StreamReader sr = new System.IO.StreamReader(originFileEN + i + ".txt"))
                {

                     text = sr.ReadToEnd();

                    foreach (string letter in letters)
                    {

                        occurrences.AppendLine(letter + " " + CountStringOccurrences(text, letter));

                    }

                    string[] Words = text.Split(',', ' ', '.', '?', '!', '"', ';', ':', '“', '”', '/');

                    Dictionary<string, int> list = new Dictionary<string, int>();

                    foreach (String entry in Words)
                    {
                        if (!list.ContainsKey(entry))
                        {
                            list.Add(entry, 1);
                        }
                        else
                        {
                            list[entry] = (int)list[entry] + 1;
                        }
                    }

                    var sortedDict = (from entry in list orderby entry.Value descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value).Take(10);

                    foreach (KeyValuePair<string, int> kvp in sortedDict)
                    {
                        if (!kvp.Key.Equals(",") || !kvp.Key.Equals(" ,") || !kvp.Key.Equals(", ") || !kvp.Key.Equals(" , ") || !kvp.Key.Equals(""))
                        {
                            string key = kvp.Key.Replace(",", "");
                            occurrences.AppendLine(key + " " + kvp.Value);

                        }
                    }

                }

                occurrences.AppendLine();

                Console.WriteLine("ficheiro " + originFileEN + i + ".txt analisado.");
                Console.WriteLine("Palavras:  " + wordsInAText(text));

            }

            File.Delete(destFileEN);

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(destFileEN, true, Encoding.UTF8))
            {

                file.WriteLine(occurrences);

            }
        }

        public static int CountStringOccurrences(string text, string pattern)
        {
            // Loop through all instances of the string 'text'.
            int count = 0;
            int i = 0;
            while ((i = text.ToLower().IndexOf(pattern.ToLower(), i)) != -1)
            {
                i += pattern.Length;
                count++;
            }
            return count;
        }

        private static int wordsInAText(string text)
        {
            return text.Split(',', ' ', '.', '?', '!', '"', ';', ':', '“', '”', '/').Length;
        }
    }
}
